/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.stdlib.numbers.function;

import axil.api.AxilObject;
import axil.api.extend.Environment;
import axil.api.extend.Function;
import axil.api.extend.Parameters;
import axil.api.extend.Source;
import axil.stdlib.numbers.type.Decimal;
import axil.stdlib.numbers.type.FloatingPoint;
import axil.stdlib.numbers.type.Int;
import axil.stdlib.numbers.type.Num;

import java.util.Random;

import static axil.framework.Functions.*;


/**
 * Produce a random number between the two given values, inclusive. The two
 * numbers given must be of the same type. Returns a random number of the same
 * type as the type of the arguments given.
 */
public class RandomF implements Function {
	private static final ThreadLocal<Random> rng = new ThreadLocal<Random>() {
		protected Random initialValue() {
			return new Random(System.currentTimeMillis());
		}
	};


	/**
	 * Execute this function with the given arguments. The Axil compiler will
	 * ensure that the number of arguments passed to the function are valid.
	 * If this function fails during execution for any reason, then an exception
	 * is thrown.
	 */
	public AxilObject eval(Environment env, Parameters params, Source source) {
		AxilObject a = params.a();
		AxilObject b = params.b();
		if (a.type() != b.type()) {
			throw error(axil(), "mismatched-types",
			            nv("first", a.type()), nv("second", b.type()));
		}
		if (a.comparedTo(b) >= 0) {
			throw error(axil(), "invalid-range", nv("begin", a), nv("end", b));
		}
		Random r = rng.get();
		switch (cast(Num.class, a).kind()) {
		case Num.INT:
			int ia = ((Int)a).value();
			int ib = ((Int)b).value();
			return Int.from(r.nextInt((ib - ia) + 1) + ia);

		case Num.FP:
			double da = ((FloatingPoint)a).fp();
			double db = ((FloatingPoint)b).fp();
			return FloatingPoint.from((r.nextDouble() * (db - da)) + da);

		case Num.DECIMAL:
			double xa = ((Decimal)a).fp();
			double xb = ((Decimal)b).fp();
			return Decimal.from((r.nextDouble() * (xb - xa)) + xa);

		default:
			throw error(axil(), "cannot-make-random", nv("type", a.type()));
		}
	}
}
